package org.mxp.controller;

import lombok.extern.slf4j.Slf4j;
import org.mxp.config.ConfirmConfig;
import org.mxp.config.DelayedQueueConfig;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * 消息生产者
 */
@Slf4j
@RestController
@RequestMapping("ttl")
public class SendMsgController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 正常消息发送
     * @param msg
     * @return
     */
    @GetMapping("sendMsg/{msg}")
    public String sendMsg(@PathVariable String msg) {
        log.info("当前时间：{}，发送一条信息给两个TTL队列：{}", new Date(), msg);
        rabbitTemplate.convertAndSend("X", "XA", "消息来自X交换机的XA路由key，内容为：" + msg);
        rabbitTemplate.convertAndSend("X", "XB", "消息来自X交换机的XB路由key，内容为：" + msg);
        return "消息发送成功";
    }

    /**
     * 样式消息，失败
     * @param msg
     * @param ttlTime
     */
    @GetMapping("sendExpirationMsg/{msg}/{ttlTime}")
    public void sendExpirationMsg(@PathVariable String msg, @PathVariable String ttlTime) {
        rabbitTemplate.convertAndSend("X", "XC", msg, correlationData -> {
            correlationData.getMessageProperties().setExpiration(ttlTime);
            return correlationData;
        });
        log.info("当前时间：{}，发送一条时长{}毫秒TTL信息给队列C：{}", new Date(), ttlTime, msg);
    }

    /**
     * 延迟消息
     * @param msg
     * @param delayTime
     */
    @GetMapping("/sendDelayMsg/{msg}/{delayTime}")
    public void sendDelayMsg(@PathVariable("msg") String msg, @PathVariable("delayTime") Integer delayTime) {
        log.info("当前时间：{}，发送一条时长{}毫秒TTL信息给延迟队列delayed.queue：{}", new Date(), delayTime, msg);
        // 发送消息
        rabbitTemplate.convertAndSend(
                DelayedQueueConfig.DELAYED_EXCHANGE_NAME,
                DelayedQueueConfig.DELAYED_ROUTING_KEY,
                msg,
                message -> {
                    // 发送消息的时候的延迟时长，单位ms
                    message.getMessageProperties().setDelay(delayTime);
                    return message;
                });
    }

    /**
     * 发布确认消息
     * @param msg
     */
    @GetMapping("/sendMessage/{msg}")
    public void sendMessage(@PathVariable("msg") String msg) throws InterruptedException {
        // 指定消息ID为UUID，交换机和路由键均存在
        String id1 = UUID.randomUUID().toString();
        CorrelationData correlationData1 = new CorrelationData(id1);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY, msg + id1, correlationData1);
        System.out.println();
        log.info("{} 发送消息内容：{}", correlationData1.getId() ,msg + id1);
        Thread.sleep(200);

        // 指定消息ID为UUID，路由键不存在
        String id2 = UUID.randomUUID().toString();
        CorrelationData correlationData2 = new CorrelationData(id2);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY + "_1", msg + id2, correlationData2);
        System.out.println();
        log.info("{} 发送消息内容：{}", correlationData2.getId() ,msg + id2);
        Thread.sleep(200);

        // 指定消息ID为UUID，路由不存在
        String id3 = UUID.randomUUID().toString();
        CorrelationData correlationData3 = new CorrelationData(id3);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME + "_1", ConfirmConfig.CONFIRM_ROUTING_KEY, msg + id3, correlationData3);
        System.out.println();
        log.info("{} 发送消息内容：{}", correlationData3.getId() ,msg + id3);
        Thread.sleep(200);
    }
}
